﻿using System;
using System.Activities.Core.Presentation;
using System.Activities.Presentation;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace Headsight.BPMN2WF.ConversionWizard
{
    /// <summary>
    /// Interaction logic for BPMN2WFConverter.xaml
    /// </summary>
    public partial class BPMN2WFConverter : UserControl, INotifyPropertyChanged
    {
        private readonly BPMN2WFViewModel _viewModel;

        #region Properties
        private string _workflowXamlContent;
        public string WorkflowXamlContent
        {
            get { return _workflowXamlContent; }
            set 
            { 
                _workflowXamlContent = value;
                NotifyPropertyChanged("WorkflowXamlContent");
            }
        }

        private string _workflowXamlTempFile;
        public string WorkflowXamlTempFile
        {
            get { return _workflowXamlTempFile; }
            set
            {
                _workflowXamlTempFile = value;
                NotifyPropertyChanged("WorkflowXamlTempFile");
            }
        }

        private bool _closeMe;
        public bool CloseMe
        {
            get { return _closeMe; }
            set
            {
                _closeMe = value;
                NotifyPropertyChanged("CloseMe");
            }
        }
        #endregion

        public BPMN2WFConverter()
        {
            InitializeComponent();

            _viewModel = new BPMN2WFViewModel();
            _viewModel.PropertyChanged += _viewModel_PropertyChanged;
            DataContext = _viewModel;

            new DesignerMetadata().Register();
        }

        private void _viewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CleanWorkflowDesigner")
            {
                CleanWorkflowDesigner();
            }
            if (e.PropertyName == "LoadWorkflowDesigner")
            {
                //CleanWorkflowDesigner();
                LoadDesigner(DesignerGrid, _viewModel.WorkflowDesigner);
            }
            if (e.PropertyName == "RefreshDesigner")
            {
                CleanWorkflowDesigner();

                var wfNew = new WorkflowDesigner();
                wfNew.Load(BPMN2WFViewModel.xamlTempFile);

                _viewModel.WorkflowDesigner = wfNew;
                LoadDesigner(DesignerGrid, _viewModel.WorkflowDesigner);
            }
            if (e.PropertyName == "CloseMe")
            {
                if (_viewModel.CloseMe)
                {
                    if (_viewModel.WorkflowDesigner == null)
                    {
                        WorkflowXamlContent = string.Empty;
                        WorkflowXamlTempFile = string.Empty;
                    }
                    else
                    {
                        WorkflowXamlTempFile = BPMN2WFViewModel.xamlTempFile;
                        _viewModel.WorkflowDesigner.Save(BPMN2WFViewModel.xamlTempFile);
                        WorkflowXamlContent = _viewModel.Wf4FileContent;
                    }
                    CloseMe = true;
                }
            }
        }

        private void CleanWorkflowDesigner()
        {
            if (_viewModel.WorkflowDesigner != null)
            {
                WorkflowXamlContent = string.Empty;
                WorkflowXamlTempFile = string.Empty;
                DesignerGrid.Children.Remove(_viewModel.WorkflowDesigner.View);
                DesignerGrid.Children.Remove(_viewModel.WorkflowDesigner.PropertyInspectorView);
                //_viewModel.WorkflowDesigner = null;
            }
        }

        private void LoadDesigner(Panel grid, WorkflowDesigner wfDesigner)
        {
            var view = wfDesigner.View;
            Grid.SetColumn(view, 0);
            grid.Children.Add(view);

            var propInspector = wfDesigner.PropertyInspectorView;
            Grid.SetColumn(propInspector, 1);
            grid.Children.Add(propInspector);
        }

        private void BtnOpenBpmn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var openFileDialog = new Microsoft.Win32.OpenFileDialog {Filter = "BPMN Project File (*.bpmn)|*.bpmn"};
                var result = openFileDialog.ShowDialog();
                if (result == true)
                {
                    var fileName = openFileDialog.FileName;
                    _viewModel.BpmnFileName = fileName;
                    var fileStream = File.OpenRead(fileName);
                    var streamReader = new StreamReader(fileStream);
                    _viewModel.BpmnFileContent = streamReader.ReadToEnd();
                    fileStream.Close();
                }
            }
            catch (Exception ex)
            {
                _viewModel.ErrorText = ex.Message;
            }
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            WorkflowXamlContent = string.Empty;
            WorkflowXamlTempFile = string.Empty;
            CloseMe = true;
        }

        private void BtnPrev_Click(object sender, RoutedEventArgs e)
        {
            _viewModel.Previous();
        }

        private void BtnNext_Click(object sender, RoutedEventArgs e)
        {
            _viewModel.Next();
        }

        private void BtnOpenMapping_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var openFileDialog = new Microsoft.Win32.OpenFileDialog
                                         {Filter = "Conversion Mapping File (*.xml)|*.xml"};
                var result = openFileDialog.ShowDialog();
                if (result == true)
                {
                    var fileName = openFileDialog.FileName;
                    _viewModel.MappingConfigurationFilename = fileName;
                    var fileStream = File.OpenRead(fileName);
                    var streamReader = new StreamReader(fileStream);
                    _viewModel.MappingConfiguration = streamReader.ReadToEnd();
                    fileStream.Close();
                }
            }
            catch (Exception ex)
            {
                _viewModel.ErrorText = ex.Message;
            }
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
